package com.hyfrogx.modules.app.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.log.LogEnum;
import com.hyfrogx.common.log.LogUtils;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.sms.SmsSingleSender;
import com.hyfrogx.common.utils.sms.SmsSingleSenderResult;
import com.hyfrogx.modules.app.dao.SmsDao;
import com.hyfrogx.modules.app.entity.Sms;
import com.hyfrogx.modules.app.service.SmsService;
import com.hyfrogx.modules.app.utils.FrogConstant;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ：Li9527
 * @date ：Created in 2019-09-16 14:15
 * @description：${description}
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class SmsServiceImpl extends ServiceImpl<SmsDao, Sms> implements SmsService {

    Logger SMS_LOG = LogUtils.Logger(LogEnum.SMS_LOGS);

    @Autowired
    SmsDao smsDao;

    @Override
    public boolean sendSms(String phone, int templId, ArrayList<String> params) {
        try {
            //初始化单发
            SmsSingleSender singleSender = new SmsSingleSender(FrogConstant.SMS_APP_ID, FrogConstant.SMS_APP_KEY);
            SmsSingleSenderResult singleSenderResult = singleSender. sendWithParam("86", phone, templId,
                    params, FrogConstant.SMS_APP_SIGNKEY, "", "");
            SMS_LOG.info("手机号：" + phone + "发送短信,结果为：" + singleSenderResult);
            if (singleSenderResult != null && singleSenderResult.result == 0){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public void createSms(Sms sms) {
        // 保存进短信表
        smsDao.insert(sms);
    }

    @Override
    public Page selectPageByConditions(QueryExt query) {
        Map map = new HashMap();
        map.putAll(query.getCondition());
        query.setRecords(smsDao.selectPageByConditions(query,map));
        return query;
    }

}
